﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Security.Cryptography;

namespace Bus_proj.website
{
    public partial class pay_for : System.Web.UI.Page
    {
        string chk_ticket, data_all;
        public static string getMd5Hash(string input)
        {
            MD5 md5Hasher = MD5.Create();
            byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));
            StringBuilder sBuilder = new StringBuilder();

            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }
            return sBuilder.ToString();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    chk_ticket = Request.QueryString["chk_ticket"];
                    if (chk_ticket == "0")
                    {
                        Panel1.Visible = false;
                        Response.Write(@"<script language='javascript'>alert('ไม่มีข้อมูลในระบบ');</script>");
                    }
                    if (chk_ticket == "1")
                    {
                        try
                        {
                            Panel1.Visible = true;
                            data_all = Request.QueryString["data_all"];
                            string[] stempb = data_all.Split('*');
                            TextBox2.Text = stempb[0];
                            TextBox7.Text = stempb[1];
                            TextBox3.Text = stempb[2];
                            TextBox4.Text = stempb[3];
                            TextBox6.Text = stempb[4];
                            TextBox8.Text = stempb[5];
                            Session["submit_all"] = stempb[0];
                        }
                        catch (Exception)
                        {
                            data_all = "0";
                        }                       
                    }
                    //if (chk_ticket == "4")
                    //{                        
                    //    Response.Write(@"<script language='javascript'>alert('ชำระเสร็จสิ้น');</script>");
                    //}
                }
                catch (Exception)
                {
                    chk_ticket = "3";
                }
            }            
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Panel1.Visible = true;
            string input = getMd5Hash(TextBox1.Text);
            Response.Redirect("pay_bus.aspx?input=" + input);
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Response.Redirect("pay_bus.aspx?submit=" + 1 + "&data_submit=" + Session["submit_all"].ToString());
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            Response.Redirect("pay_for.aspx");
        }
    }
}